	package com.geekchique.runningbuddy.controller;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedHashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;

import com.geekchique.runningbuddy.activity.DayOfJogDetails;
import com.geekchique.runningbuddy.model.Jog;
import com.geekchique.runningbuddy.model.ListOfJogs;
import com.geekchique.runningbuddy.model.PastJogsCalendar;
import com.geekchique.runningbuddy.provider.ActivityID;
import com.geekchique.runningbuddy.util.DBHelperImpl;
import com.geekchique.runningbuddy.view.PastjogsView;

public class PastJogsControllerImpl implements IPastJogsController{
	
	@SuppressWarnings("unused")
	private Activity activity;
	private PastjogsView pastJogsView;
	private PastJogsCalendar pastJogsCalendar;
	
	private DBHelperImpl dbHelper;
	private Calendar showing;
	private Context pastJogsContext;

	public void createControls(
			Activity pastJogs,
			Context pastJogsC,
			Button goToToday,
			TextView currMonth,
			Button dayContext, Button monthContext, Button yearContext, Button backward, Button forward) {

				this.pastJogsCalendar = new PastJogsCalendar();
				activity = pastJogs;
				pastJogsContext = pastJogsC;
				
				this.pastJogsView = new PastjogsView(pastJogs, this, goToToday, currMonth, dayContext, monthContext, yearContext, backward, forward);
				int year = this.pastJogsCalendar.get(Calendar.YEAR);
				int month = this.pastJogsCalendar.get(Calendar.MONTH);
				int day = this.pastJogsCalendar.get(Calendar.DAY_OF_MONTH);
				updateCalendarModel(pastJogsCalendar, year, month, day);
				showing = new GregorianCalendar();
	}

	public void createCalendar(
			TableLayout calendarTable,
			Button cell00,
			Button cell01,
			Button cell02,
			Button cell03,
			Button cell04,
			Button cell05,
			Button cell06,
			Button cell10,
			Button cell11,
			Button cell12,
			Button cell13,
			Button cell14,
			Button cell15,
			Button cell16,
			Button cell20,
			Button cell21,
			Button cell22,
			Button cell23,
			Button cell24,
			Button cell25,
			Button cell26,
			Button cell30,
			Button cell31,
			Button cell32,
			Button cell33,
			Button cell34,
			Button cell35,
			Button cell36,
			Button cell40,
			Button cell41,
			Button cell42,
			Button cell43,
			Button cell44,
			Button cell45,
			Button cell46,
			Button cell50,
			Button cell51,
			Button cell52,
			Button cell53,
			Button cell54,
			Button cell55,
			Button cell56) {

		LinkedHashMap<Integer, Button> tableCells = new LinkedHashMap<Integer, Button>();
		dbHelper = new DBHelperImpl(pastJogsContext);

		createTableCellsCollection(cell00, cell01, cell02, cell03, cell04,
				cell05, cell06, cell10, cell11, cell12, cell13, cell14, cell15,
				cell16, cell20, cell21, cell22, cell23, cell24, cell25, cell26,
				cell30, cell31, cell32, cell33, cell34, cell35, cell36, cell40,
				cell41, cell42, cell43, cell44, cell45, cell46, cell50, cell51,
				cell52, cell53, cell54, cell55, cell56, tableCells);
		
		
		this.pastJogsView.setCellsInCalendarTable(tableCells);
		this.pastJogsView.updateCalendarView(pastJogsCalendar);
		this.pastJogsView.removeAllCellHighlights();
		
    	highlightAllDaysJoggedOnView();
		highLightTodayOnView();
		
		this.pastJogsView.addDayClickListeners(tableCells);
	}

	private void createTableCellsCollection(Button cell00, Button cell01,
			Button cell02, Button cell03, Button cell04, Button cell05,
			Button cell06, Button cell10, Button cell11, Button cell12,
			Button cell13, Button cell14, Button cell15, Button cell16,
			Button cell20, Button cell21, Button cell22, Button cell23,
			Button cell24, Button cell25, Button cell26, Button cell30,
			Button cell31, Button cell32, Button cell33, Button cell34,
			Button cell35, Button cell36, Button cell40, Button cell41,
			Button cell42, Button cell43, Button cell44, Button cell45,
			Button cell46, Button cell50, Button cell51, Button cell52,
			Button cell53, Button cell54, Button cell55, Button cell56,
			LinkedHashMap<Integer, Button> tableCells) {
		for(int i = 0; i < 45; i++){
			tableCells.put(00, cell00);
			tableCells.put(01, cell01);
			tableCells.put(02, cell02);
			tableCells.put(03, cell03);
			tableCells.put(04, cell04);
			tableCells.put(05, cell05);
			tableCells.put(06, cell06);
			tableCells.put(10, cell10);
			tableCells.put(11, cell11);
			tableCells.put(12, cell12);
			tableCells.put(13, cell13);
			tableCells.put(14, cell14);
			tableCells.put(15, cell15);
			tableCells.put(16, cell16);
			tableCells.put(20, cell20);
			tableCells.put(21, cell21);
			tableCells.put(22, cell22);
			tableCells.put(23, cell23);
			tableCells.put(24, cell24);
			tableCells.put(25, cell25);
			tableCells.put(26, cell26);
			tableCells.put(30, cell30);
			tableCells.put(31, cell31);
			tableCells.put(32, cell32);
			tableCells.put(33, cell33);
			tableCells.put(34, cell34);
			tableCells.put(35, cell35);
			tableCells.put(36, cell36);
			tableCells.put(40, cell40);
			tableCells.put(41, cell41);
			tableCells.put(42, cell42);
			tableCells.put(43, cell43);
			tableCells.put(44, cell44);
			tableCells.put(45, cell45);
			tableCells.put(46, cell46);
			tableCells.put(50, cell50);
			tableCells.put(51, cell51);
			tableCells.put(52, cell52);
			tableCells.put(53, cell53);
			tableCells.put(54, cell54);
			tableCells.put(55, cell55);
			tableCells.put(56, cell56);
		}
	}

	protected void highlightAllDaysJoggedOnView() {
		Calendar calendar = pastJogsCalendar.getCalendar();
    	ListOfJogs entries = (ListOfJogs)dbHelper.fetchAllThisMonthsJogEntries(calendar);
		highLightCalendarDays(entries);
	}
	
	private void highLightCalendarDays(ListOfJogs entries) {
		int day; 
		
		for(Iterator<Jog>iter = entries.iterator(); iter.hasNext();){
			Jog jog = iter.next();
			day = jog.getStopTimeCal().get(Calendar.DAY_OF_MONTH);
			Integer dayindex = Integer.valueOf(pastJogsCalendar.getTableIndexOfDay(day));
			pastJogsView.setAHighightedDayOfJogging(dayindex);
		}
	}

	private void updateCalendar(int ammendmentType, int ammendment) {
		ammendCurrentlyShowingDate(pastJogsCalendar, ammendmentType, ammendment);
	}

	public void ammendCurrentlyShowingDate(int ammendmentType, int ammendment) {
		ammendCurrentlyShowingDate(pastJogsCalendar, ammendmentType, ammendment);
	}
	
	private void ammendCurrentlyShowingDate(PastJogsCalendar pastJogsCalendar, int ammendmentType, int ammendment) {
		Calendar now = new GregorianCalendar();
		
		switch(ammendmentType){
			case Calendar.YEAR:	
								updateCalendarModel(pastJogsCalendar, pastJogsCalendar.getCalendar().get(Calendar.YEAR) + ammendment, pastJogsCalendar.getCalendar().get(Calendar.MONTH), pastJogsCalendar.getCalendar().get(Calendar.DAY_OF_MONTH));
								break;
			
			case Calendar.MONTH:
								updateCalendarModel(pastJogsCalendar, pastJogsCalendar.getCalendar().get(Calendar.YEAR), pastJogsCalendar.getCalendar().get(Calendar.MONTH) + ammendment, pastJogsCalendar.getCalendar().get(Calendar.DAY_OF_MONTH));
								break;
			
			case Calendar.DAY_OF_MONTH:
								updateCalendarModel(pastJogsCalendar, pastJogsCalendar.getCalendar().get(Calendar.YEAR), pastJogsCalendar.getCalendar().get(Calendar.MONTH), pastJogsCalendar.getCalendar().get(Calendar.DAY_OF_MONTH) + ammendment);
								break;
		}
		
		pastJogsView.updateCalendarView(pastJogsCalendar);
		showing.setTimeInMillis(pastJogsCalendar.getCurrentlyShowingDate());
		
		this.pastJogsView.removeAllCellHighlights();
		highlightAllDaysJoggedOnView();
		
		if(showing.get(Calendar.MONTH) == now.get(Calendar.MONTH) && showing.get(Calendar.YEAR) == now.get(Calendar.YEAR)){
			highLightTodayOnView();
		}
		
	}

	public void updateCalendarModel(PastJogsCalendar pastJogsCalendar, int year, int month, int day) {
		pastJogsCalendar.setCurrentMonthTable(year, month, day);
	}

	public void highLightTodayOnView() {
		int day = pastJogsCalendar.get(Calendar.DAY_OF_MONTH);
		Integer dayindex = Integer.valueOf(pastJogsCalendar.getTableIndexOfDay(day));
		pastJogsView.setCurrentlySelectedDay(dayindex);
	}
	
	
	protected Handler pastJogsCalendarServiceHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ActivityID.ADD_YEAR:
				updateCalendar(Calendar.YEAR, 1);
				break;
			case ActivityID.ADD_MONTH:
				updateCalendar(Calendar.MONTH, 1);
				break;
			case ActivityID.ADD_DAY:
				updateCalendar(Calendar.DAY_OF_MONTH, 1);
				break;
			case ActivityID.MINUS_DAY:
				updateCalendar(Calendar.DAY_OF_MONTH, -1);
				break;
			case ActivityID.MINUS_MONTH:
				updateCalendar(Calendar.MONTH, -1);
				break;
			case ActivityID.MINUS_YEAR:
				updateCalendar(Calendar.YEAR, -1);
				break;
			}
			super.handleMessage(msg);
		}
	};

	public void loadDayDetails(String dayOfMonth) {
		
        Intent intent = new Intent(activity,  DayOfJogDetails.class);
        
        // Java util month is zero based
        Integer month = pastJogsCalendar.get(Calendar.MONTH);
        month++;
        
        Bundle bundle = new Bundle(2);
        bundle.putInt(ActivityID.DAY_OF_MONTH,  Integer.parseInt(dayOfMonth));
        bundle.putInt(ActivityID.MONTH_OF_YEAR, month);
        bundle.putInt(ActivityID.YEAR, pastJogsCalendar.get(Calendar.YEAR));
        
        intent.putExtras(bundle);
        
        activity.startSubActivity(intent, ActivityID.SHOW_DAY_DETAILS);
	}

}
